    package com.diudiutechnology.www.yadiudiu.util.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.diudiutechnology.www.yadiudiu.util.view.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

    /**
     * MyImageview 工具
     */
    public class ImageLoaderUtil {
        private static ImageLoaderConfiguration IMImageLoaderConfig;
        private static ImageLoader IMImageLoadInstance;
        private static Map<Integer, Map<Integer, DisplayImageOptions>> avatarOptionsMaps = new HashMap<Integer,
                Map<Integer, DisplayImageOptions>>();
        public final static int CIRCLE_CORNER = -10;

        public static File cacheDir,reserveCacheDir;

        public static void initImageLoaderConfig(Context context) {
            try {
                cacheDir = StorageUtils.getOwnCacheDirectory(context, FileUtils.getSavePath(Constant
                        .FILE_SAVE_TYPE_IMAGE));
                reserveCacheDir = StorageUtils.getCacheDirectory(context);

                int maxMemory = (int) (Runtime.getRuntime().maxMemory());
                // 使用最大可用内存值的1/8作为缓存的大小。
                int cacheSize = maxMemory / 8;

                DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                        .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
                        .build();

                IMImageLoaderConfig = new ImageLoaderConfiguration.Builder(context)
                        .memoryCacheExtraOptions(480, 800)
                        .threadPriority(Thread.NORM_PRIORITY - 2)
                        .denyCacheImageMultipleSizesInMemory()
                        .threadPoolSize(5)
                        .memoryCache(new UsingFreqLimitedMemoryCache(cacheSize))
                        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                        .tasksProcessingOrder(QueueProcessingType.LIFO)
                        .diskCacheExtraOptions(480, 800, null)
                        .diskCache(new UnlimitedDiscCache(cacheDir, reserveCacheDir, new Md5FileNameGenerator()))
                        .diskCacheSize(1024 * 1024 * 1024)
                        .diskCacheFileCount(1000)
                        .defaultDisplayImageOptions(options)
                        .writeDebugLogs()
                        .build();

                IMImageLoadInstance = ImageLoader.getInstance();
                IMImageLoadInstance.init(IMImageLoaderConfig);
            } catch (Exception e) {
            }
        }

        public static ImageLoader getImageLoaderInstance() {
            return IMImageLoadInstance;
        }


        public static DisplayImageOptions getAvatarOptions(int corner, int defaultRes) {
            try {
                if (defaultRes <= 0) {
                    defaultRes = Constant.IC_LAUNCHER;
                }
                if (avatarOptionsMaps.containsKey(defaultRes)) {
                    Map<Integer, DisplayImageOptions> displayOption = avatarOptionsMaps.get(defaultRes);
                    if (displayOption.containsKey(corner)) {
                        return displayOption.get(corner);
                    }
                }
                DisplayImageOptions newDisplayOption = null;
                if (corner == CIRCLE_CORNER) {
                    newDisplayOption = new DisplayImageOptions.Builder()
                            .showImageOnFail(defaultRes)
                            .showImageForEmptyUri(defaultRes)
                            .cacheInMemory(true)
                            .resetViewBeforeLoading(true)
                            .displayer(new CircleBitmapDisplayer())
                            .build();
                } else {
                    if (corner < 0) {
                        corner = 0;
                    }
                    newDisplayOption = new DisplayImageOptions.Builder()
                            .cacheInMemory(true)
                            .cacheOnDisk(true)
                            .build();
                }

                Map<Integer, DisplayImageOptions> cornerDisplayOptMap = new HashMap<Integer, DisplayImageOptions>();
                cornerDisplayOptMap.put(corner, newDisplayOption);
                avatarOptionsMaps.put(defaultRes, cornerDisplayOptMap);
                return newDisplayOption;
            } catch (Exception e) {
                return null;
            }
        }

        /**
         * 清除缓存
         */
        public static void clearCache() {
            try {
                if (IMImageLoadInstance != null) {
                    IMImageLoadInstance.clearMemoryCache();
                    IMImageLoadInstance.clearDiskCache();
                }
                if (null != avatarOptionsMaps) {
                    avatarOptionsMaps.clear();
                }
            } catch (Exception e) {
            }
        }
    }
